package com.ldc.api.memu;

import com.ldc.common.utils.CollectionUtils;
import com.ldc.model.SysMenu;
import com.ldc.model.SysRole;
import com.ldc.model.SysRoleMenu;
import com.ldc.model.base.ApiBean;
import com.ldc.service.service.MenuService;
import com.ldc.service.service.RoleMenuService;
import com.ldc.service.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/22
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    RoleService roleService;

    /**
     * 获取权菜单列表
     *
     * @param params
     * @return
     */
    @PostMapping("/list")
    public ApiBean<?> menuList(@RequestBody List<SysRole> params) {
        // 获取role
        List<String> roleList = params.stream().map(SysRole ::getRole).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(roleList)) {
            return ApiBean.failed(new ArrayList<>());
        }
        // roleId
        List<Long> roleIdList = roleService.findRoleIdListByRoles(roleList);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return ApiBean.failed(new ArrayList<>());
        }
        // menuId
        List<Long> menuIds = roleMenuService.getMenuIdsByRoleIds(roleIdList);
        if (CollectionUtils.isEmpty(menuIds)) {
            return ApiBean.failed(new ArrayList<>());
        }
        List<SysMenu> menuList = menuService.findListByMenuId(menuIds);
        return ApiBean.success(menuList);
    }

    /**
     * 批量删除
     *
     * @param menuIds
     * @return
     */
    @PostMapping(value = "/delete")
    public ApiBean<?> delete(@RequestBody List<Long> menuIds) {
        if (CollectionUtils.isEmpty(menuIds)) {
            return ApiBean.failed(new ArrayList<>());
        }
        int code = menuService.batchDeleteByMenuIds(menuIds);
        List<SysRoleMenu> roleMenuBeanList = roleMenuService.findListByMenuIds(menuIds);
        if (code > 0 && CollectionUtils.isNotEmpty(roleMenuBeanList)) {
            // 删除映射关系
            List<Long> _menuIds = roleMenuBeanList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
            // 删除成功
            int successCode = roleMenuService.batchDeleteByMenuIds(_menuIds);
            return successCode > 0 ? ApiBean.success(_menuIds) : ApiBean.failed(new ArrayList<>());
        } else if (code > 0) {
            return ApiBean.success(menuIds);
        }
        return ApiBean.failed(new ArrayList<>());
    }

    /**
     * 添加菜单
     *
     * @param menuBeans
     * @return
     */
    @PostMapping("/add")
    public ApiBean<?> add(@RequestBody List<SysMenu> menuBeans) {
        if (CollectionUtils.isEmpty(menuBeans)) {
            return ApiBean.failed(new ArrayList<>());
        }
        boolean success = menuService.batchAdd(menuBeans);
        return success ? ApiBean.success(menuBeans) : ApiBean.failed(new ArrayList<>());
    }
}
